package com.example.picturestorage.mapper;

import com.example.picturestorage.bean.Book;
import com.example.picturestorage.bean.Publisher;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@CacheNamespace
@Repository
public interface PublisherMapper {

    @Insert("insert into tb_publisher(publisher_name) values(#{publisherName})")
    @Options(useGeneratedKeys = true, keyProperty = "id", useCache = false)
    void addPublisher(Publisher publisher);

    @Select("select * from tb_publisher")
    @Results(id = "publisher",
            value = {
                    @Result(property = "id", column = "id", id = true),
                    @Result(property = "publisherName", column = "publisher_name")
            })
    List<Publisher> findAllPublisher();

//    @Select("select * from tb_publisher")
//    @Results(id = "publisher2",
//            value = {
//                    @Result(property = "id", column = "id", id = true),
//                    @Result(property = "publisherName", column = "publisher_name"),
//                    @Result(property = "books.book.bookName", column = "book_name"),
//                    @Result(property = "books", column = "id", javaType = List.class,
//                            many = @Many(select = "com.example.picturestorage.mapper.BookMapper.findAllBookByPubId"))
//            })
    List<Publisher> findAllPublisherWithBooks();

    @Update("update tb_publisher set publisher_name = #{publisherName} where id = #{id}")
    void updatePublisher(String publisherName, Integer id);

    @Delete("delete from tb_publisher where id = #{id}")
    void deletePublisher(Integer id);

    @Select("select * from tb_publisher where id = #{id}")
    @ResultMap(value = "publisher") //可以直接引用现有的resultMap
    Publisher findPublisherById(Integer id);

    @Select("select * from tb_publisher where publisher_name like concat('%',#{publisherName},'%')")
    Publisher findPublisherByName(String publisherName);

    int insertPublishers(List<Publisher> publishers);

}
